#include "Attendance.h"
#include "ui_Attendance.h"
#include <QMessageBox>
#include <QtSql>
#include "Teacher/TeacherHome.h"

Attendance::Attendance(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Attendance)
{
    ui->setupUi(this);

}

void Attendance::SetTeacher(const QString &TeaID)
{
    this->TeaID=TeaID;
}
Attendance::~Attendance()
{
    delete ui;
}


void Attendance::displayAttendance()
{
    // 创建SQL查询对象
    QSqlQuery query;

    // 第一步：通过老师邮箱查询老师的姓名
    query.prepare("SELECT `姓名` FROM  `教师表` WHERE `邮箱` = :TeaID");
    query.bindValue(":TeaID",TeaID );
    if (!query.exec()) {
        QMessageBox::critical(this, "Error", "Failed to query teacher name!");
        return;
    }

      QString teacherName;
      if (query.next()) {
          teacherName = query.value(0).toString();  // 获取教师姓名
      } else {
          QMessageBox::critical(this, "Error", "Teacher not found!");
          return;
      }


    // 第二步：获取学生学号并查询考勤表

          query.prepare("SELECT `学号` FROM `学生表` WHERE `任课老师` = :teacherName");
          query.bindValue(":teacherName", teacherName);  // 绑定教师姓名
          if (!query.exec()) {
              QMessageBox::critical(this, "Error", "Failed to query students!");
              return;
          }
          //清空表格内容
      ui->tableWidget->setRowCount(0);

    while (query.next()) {
        QString studentId = query.value(0).toString();

        // 通过学生学号查询考勤表
        QSqlQuery attendanceQuery;
        attendanceQuery.prepare("SELECT `学号`, `上午签到时间`, `下午签退时间`, `日期`, `考勤状态` "
                                "FROM `考勤表` WHERE `学号` = :`学号` ORDER BY `日期` DESC");
        attendanceQuery.bindValue(":`学号`", studentId);

        if (!attendanceQuery.exec()) {
            QMessageBox::critical(this, "Error", "Failed to query attendance!");
            return;
        }

        // 第三步：更新界面上的考勤记录（假设使用QTableWidget）
        while (attendanceQuery.next()) {
            int row = ui->tableWidget->rowCount();
            ui->tableWidget->insertRow(row);

            // 填充考勤数据
            ui->tableWidget->setItem(row, 0, new QTableWidgetItem(attendanceQuery.value(0).toString())); // 学生学号
            ui->tableWidget->setItem(row, 1, new QTableWidgetItem(attendanceQuery.value(1).toString())); // 上午签到时间
            ui->tableWidget->setItem(row, 2, new QTableWidgetItem(attendanceQuery.value(2).toString())); // 下午签退时间
            ui->tableWidget->setItem(row, 3, new QTableWidgetItem(attendanceQuery.value(3).toString())); // 日期

            // 根据考勤状态显示相应的考勤情况
            int status = attendanceQuery.value(4).toInt();
            QString statusText;
            switch (status) {
                case 0:
                    statusText = "缺勤";
                    break;
                case 1:
                    statusText = "签到";
                    break;
                case 2:
                    statusText = "请假";
                    break;
                default:
                    statusText = "未知";
                    break;
            }
            ui->tableWidget->setItem(row, 4, new QTableWidgetItem(statusText)); // 考勤情况

            // 调用 TeacherHome 的 insertAttendanceData 方法，将数据传递过去
                        QStringList rowData;
                        rowData << studentId
                                << attendanceQuery.value(1).toString()  // 上午签到时间
                                << attendanceQuery.value(2).toString()  // 下午签退时间
                                << attendanceQuery.value(3).toString()  // 日期
                                << statusText;  // 考勤状态
                         static_cast<TeacherHome*>(parent())->insertAttendanceData(rowData);  // 传递数据给 TeacherHome
                    }
        }
    }


